import { IHistory } from "model/DB"


export function getFileNameByUrl(url: string) {
  const temp = url.includes('?') ? url.substring(0, url.indexOf('?')) : url
  const start = url.lastIndexOf('/')
  const end = url.indexOf('.', start)
  
  return end !== -1 ? temp.substring(start+1, end) : temp.substring(start+1)
}


export function parseTime(dur: number) {
  // 将秒转成 时分
  const h = Math.floor(dur/3600)
  const m = Math.round((dur%3600)/60)

  let result = ''

  if(h > 0) {
    result = `${h}小时`
  }
  if(m > 0) {
    result = `${result}${m}分`
  }
  
  return result
}


export function getPlayerUrl(task: IHistory | undefined, port: number) {
  // console.log('task', task)
  let localUrl = task && task.localUrl ? task.localUrl : ''
  if(localUrl === '') {
    return localUrl
  }
  const host = 'http://localhost'
  if(localUrl.indexOf(host) === 0) {
    localUrl = localUrl.substring(localUrl.indexOf('/', host.length))
  }
  return `${host}:${port}${localUrl}`
}



// const get_userid_array = function (type) {
//   let map_filename = {
//       'nasagent': 'nasagent'
//   };
//   let exists = fs.existsSync(map_filename[type]);
//   if (!exists) {
//       return ['poolname', 'username'];
//   }
//   else {
//       let result = JSON.parse(fs.readFileSync(map_filename[type]));
//       if ('userid_array' in result) {
//           return result['userid_array'];
//       }
//       else {
//           return ['poolname', 'username'];
//       }
//   }
// }